package fristphase.day9_07.my_plane_battle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class World extends JPanel{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 700;
    private static final int GAME_OVER = 0;
    private static final int RUNNING = 1;
    private static final int PAUSE = 2;
    private static final int START = 3;
    private int state = START;
    private HeroPlane heroPlane = new HeroPlane();
    private FlyingObject[] enemyPlanes = {};
    private Bullet[] bullets = {};
    private Sky sky = new Sky();

    //生产敌机
    private FlyingObject generateEnemyPlane(){
        Random random = new Random();
        int type = random.nextInt(20);
        if(type < 10){
            return   new SmallEnemyPlane();
        }else if(type < 15){
            return new BigEnemyPlane();
        }else {
            return new Bee();
        }
    }

    //敌机入场
    int enemyPlaneIndex = 0;
    private void enemyPlaneEnter(){
        enemyPlaneIndex ++;
        if(enemyPlaneIndex%40 == 0){
            enemyPlanes = Arrays.copyOf(enemyPlanes,enemyPlanes.length+1);
            enemyPlanes[enemyPlanes.length-1] = generateEnemyPlane();
        }
    }

    //子弹入场
    int bulletIndex = 0;
    private void bulletEnter(){
        bulletIndex ++;
        if (bulletIndex%10 == 0){
            Bullet[] bs = heroPlane.shootBullet();
            if(bs.length>1){
                bullets = Arrays.copyOf(bullets,bullets.length+2);
                bullets[bullets.length-1] = bs[0];
                bullets[bullets.length-2] = bs[1];
            }else {
                bullets = Arrays.copyOf(bullets,bullets.length+1);
                bullets[bullets.length-1] = bs[0];
            }

        }
    }



    //飞行物移动
    private void move(){
        sky.step();
        for (int i = 0; i < enemyPlanes.length; i++) {
            if(enemyPlanes[i] instanceof Bee){
                enemyPlanes[i].step();
            }else{
                enemyPlanes[i].step();
            }
        }
        for (int i = 0; i < bullets.length; i++) {
            bullets[i].step();
        }
    }

    //检测是否删除对象
    private void isOutBoundOf(){
        for (int i = 0; i < enemyPlanes.length; i++) {
            FlyingObject eps = enemyPlanes[i];
            if(eps.enemyPlaneOutBoundOf() || eps.getRemove()){//检测敌机是否越界和死亡
                enemyPlanes[i] = enemyPlanes[enemyPlanes.length-1];
                enemyPlanes = Arrays.copyOf(enemyPlanes,enemyPlanes.length-1);
            }
        }
        for (int i = 0; i < bullets.length; i++) {
            Bullet b = bullets[i];
            if(b.bulletOutBoundsOf() || b.getDead()){//检测子弹是否越界与死亡
                bullets[i] = bullets[bullets.length-1];
                bullets = Arrays.copyOf(bullets,bullets.length-1);
            }

        }
    }

    //子弹与敌人碰撞
    int score = 0;
    private void bulletBangAction(){
        for (int i = 0; i < bullets.length; i++) {
            Bullet b = bullets[i];
            for (int j = 0; j < enemyPlanes.length; j++) {
                FlyingObject ep = enemyPlanes[j];
                if(ep.isHit(b) && ep.getLive() && b.getLive()){
                    b.goDead();
                    ep.goDead();
                    if(ep instanceof EnemyScore){
                        EnemyScore obj = (EnemyScore) ep;
                        score += obj.getScore();
                    }else if(ep instanceof EnemyAward){
                        EnemyAward obj = (EnemyAward) ep;
                        heroPlane.getAward(obj.getAwardType());
                    }
                }
            }

        }
    }

    //英雄机与敌人碰撞
    private void heroPlaneIsHit(){
        for (int i = 0; i < enemyPlanes.length; i++) {
            FlyingObject ep = enemyPlanes[i];
            if(ep.isHit(heroPlane) && ep.getLive() && heroPlane.getLive()){
                ep.goDead();
                heroPlane.subtractLife(ep.enemyPlaneSubtractLife());
            }
        }
    }

    //检测游戏结束
    private void gameOver(){
        if(heroPlane.getLife()<=0){
            this.state = GAME_OVER;
        }
    }




    //总活动管理
    private void action(){
        MouseAdapter ms = new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                if(state == RUNNING){
                    heroPlane.moveTo(x,y);
                }

            }
            public void mouseEntered(MouseEvent e) {
                if(state == PAUSE){
                        state = RUNNING;
                    }
            }
            public void mouseExited(MouseEvent e) {
                if(state == RUNNING){
                    state = PAUSE;
                }
            }
            public void mouseClicked(MouseEvent e) {
                //GAME_OVER = 0,RUNNING = 1,PUASE = 2,START = 3,
                switch (state){
                    case 0:
                        enemyPlanes = new FlyingObject[0];
                        bullets = new Bullet[0];
                        heroPlane = new HeroPlane();
                        score = 0;
                        state = START;
                        break;
                    case 3:
                        state = RUNNING;
                }
            }
        };
        this.addMouseListener(ms);//鼠标点击监听
        this.addMouseMotionListener(ms);//鼠标滑动监听



        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                if (state == RUNNING){
                    enemyPlaneEnter();//敌机入场
                    bulletEnter();//子弹入场
                    move();//飞行物移动功能加入
                    isOutBoundOf();//检测出界
                    bulletBangAction();//子弹碰撞功能
                    heroPlaneIsHit();//英雄机与敌人碰撞
                    gameOver();//检测游戏结束

                }repaint();


            }
        },interval,interval);

    }
    //画天空的对象
    public void paint(Graphics g){
//        Images.background.paintIcon(null,g,0,0);
        g.drawImage(sky.getImage().getImage(), sky.x,sky.y,null);
        g.drawImage(sky.getImage().getImage(),sky.x,sky.getY1(),null );
        sky.paintImage(g);
            heroPlane.paintImage(g);
        for (int i = 0; i < enemyPlanes.length; i++) {
            enemyPlanes[i].paintImage(g);
        }
        for (int i = 0; i < bullets.length; i++) {
            bullets[i].paintImage(g);
        }
        g.drawString("SCORE："+this.score,100,50);
        g.drawString("LIFE："+heroPlane.getLife(),200,50);
        if(state == GAME_OVER){
            Images.gameover.paintIcon(null,g,0,0);
        }
        if(state == PAUSE){
            Images.pause.paintIcon(null,g,0,0);
        }
        if(state == START){
            Images.start.paintIcon(null,g,0,0);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        frame.add(world);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setSize(WIDTH,HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        world.action();
    }

}
